package com.xinfu.newproject.ui.carFee.add_parking;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;

import com.google.gson.Gson;
import com.xinfu.newproject.R;
import com.xinfu.newproject.activity.WebPayActivity;
import com.xinfu.newproject.base.BaseActivity;
import com.xinfu.newproject.bean.login.LoginBean;
import com.xinfu.newproject.bean.park_bean.bean.AddMonthCarBean;
import com.xinfu.newproject.bean.park_bean.bean.CheckMonthParkBean;
import com.xinfu.newproject.bean.park_bean.bean.FeeListBean;
import com.xinfu.newproject.bean.park_bean.bean.GetMonthCarListBean;
import com.xinfu.newproject.bean.park_bean.bean.GetParkingBean;
import com.xinfu.newproject.bean.park_bean.bean.MonthCarPayBean;
import com.xinfu.newproject.bean.park_bean.bean.PaymentsBean;
import com.xinfu.newproject.bean.paycost.bean.ComputeParkingBean;
import com.xinfu.newproject.presenter.ParkingPresenter;
import com.xinfu.newproject.util.BToast;
import com.xinfu.newproject.util.DateFormatUtils;
import com.xinfu.newproject.util.SPUtils;
import com.xinfu.newproject.view.IView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

import static com.xinfu.newproject.contants.Constant.LOGIN;

public class ParkingTopUpActivity extends BaseActivity implements IView.IParkingView, IView.IAppIntoPayView {

    @BindView(R.id.activity_month_park_top_up_community)
    TextView communityTv;

    @BindView(R.id.activity_month_park_top_up_name)
    TextView nameTv;

    @BindView(R.id.activity_month_park_top_up_car)
    TextView carTv;

    @BindView(R.id.activity_month_park_top_up_end_time)
    TextView endTimeTv;

    @BindView(R.id.activity_month_park_top_up_start_time)
    TextView startTimeTv;

    @BindView(R.id.activity_month_park_top_up_month)
    TextView monthTv;

    @BindView(R.id.activity_month_park_top_up_month_tv)
    TextView renewMonthTv;

    @BindView(R.id.activity_month_park_top_up_pay)
    TextView payTv;

    @BindView(R.id.activity_month_par_top_upk_money)
    TextView moneyTv;

    @BindView(R.id.activity_month_park_top_up_money_btn)
    TextView moneyBtn;

    @BindView(R.id.activity_month_park_top_up_instruction)
    TextView instructionTv;

    private ParkingPresenter parkingPresenter = new ParkingPresenter((IView.IParkingView) this);
    private ParkingPresenter parkingPresenter2 = new ParkingPresenter((IView.IAppIntoPayView) this);
    String[] items = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_month_park_top_up);
        ButterKnife.bind(this);
        registerBack();
        setActivityTitle("月卡充值");
        initViewBind();
        initLisener();

    }

    private void initLisener() {

    }

    GetMonthCarListBean.ResultBean item;

    private void initViewBind() {
        item = new Gson().fromJson(getIntent().getExtras().getString("response"), GetMonthCarListBean.ResultBean.class);
//        BToast.showText(this, getIntent().getExtras().getString("response"));
        if (isLogin()) {
            tenantId = item.getTenantId();
            parkingPresenter.payments();
        }
    }

    double realMoney;
    String realMonth;
    int position;
    @OnClick({R.id.activity_month_park_top_up_month, R.id.activity_month_park_top_up_money_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.activity_month_park_top_up_month:
                if (items != null) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setIcon(android.R.drawable.alert_dark_frame);
                    builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            monthTv.setText(items[which]);
                            renewMonthTv.setVisibility(View.VISIBLE);
                            renewMonthTv.setText(DateFormatUtils.getTime(payment.getResult().getParkFees().get(which).getChargeDateEnd()));
                            payTv.setText((payment.getResult().getParkFees().get(which).getRealMoney() / 100) + "元");
                            moneyTv.setText((payment.getResult().getParkFees().get(which).getRealMoney() / 100) + "元");
                            instructionTv.setText(payment.getResult().getParkFees().get(which).getRemarks());
                            realMoney = payment.getResult().getParkFees().get(which).getRealMoney();
                            realMonth = items[which];
                            position = which;
                            dialog.dismiss();
                        }
                    });
                    builder.show();
                }
                break;
            case R.id.activity_month_park_top_up_money_btn:
                if (payment != null) {
                    if (moneyTv.getText().toString().equals("0.0")) {
                        BToast.showText(this, "请先选择充值月数");
                    } else {
                        parkingPresenter2.appIntoPay();
                    }
                }
                break;
        }
    }

    /**
     * 判断是否可以请求数据
     */
    String token, userId, userName, tenantId, phone;
    int urlType;

    private boolean isLogin() {

        LoginBean.ResultBean resultBean;
        String responseIntent = (String) SPUtils.get(this, LOGIN, "0");
        if (!responseIntent.equals("0")) {
            resultBean = new Gson().fromJson(responseIntent, LoginBean.ResultBean.class);
            token = resultBean.getToken();
            userId = resultBean.getUserInfo().getId();
            userName = resultBean.getUserInfo().getUsername();
            phone = resultBean.getUserInfo().getPhone();
        } else {
            BToast.showText(this, "请先登录");
            return false;
        }

        return true;
    }


    @Override
    public String header() {
        return tenantId;
    }

    @Override
    public String userId() {
        return userId;
    }

    @Override
    public String parkInfoId() {
        return null;
    }

    @Override
    public String carId() {
        return payment.getResult().getParkCarExt().getCarId();
    }

    @Override
    public Integer carType() {
        return payment.getResult().getParkCarExt().getCarType();
    }

    @Override
    public String parkCarId() {
        return payment.getResult().getParkCarExt().getId();
    }

    @Override
    public String projectId() {
        return payment.getResult().getParkCarExt().getProjectId();
    }

    @Override
    public Integer tenantId() {
        return Integer.valueOf(payment.getResult().getParkCarExt().getTenantId());
    }

    @Override
    public String exclusiveFlag() {
        return null;
    }

    @Override
    public String monthCarType() {
        return null;
    }

    @Override
    public String roomId() {
        return null;
    }

    @Override
    public String owner() {
        return null;
    }

    @Override
    public String mobile() {
        return phone;
    }

    @Override
    public String images() {
        return null;
    }

    @Override
    public String remark() {
        return null;
    }

    @Override
    public Integer idType() {
        return null;
    }

    @Override
    public String filed1() {
        return null;
    }

    @Override
    public String id() {
        return item.getId();
    }

    String stratTime;
    @Override
    public String startTime() {
        if (payment == null) {
            stratTime = item.getStartTime();
        } else {
            stratTime = DateFormatUtils.getTimeSS(payment.getResult().getStartChargeDate());
        }
        return stratTime;
    }

    @Override
    public Integer num() {
        return payment.getResult().getParkFees().get(position).getRenMonth();
    }

    @Override
    public Integer payAmount() {

        return (int) realMoney;
    }

    @Override
    public String payMemo() {
        return payment.getResult().getParkFees().get(position).getRemarks();
    }

    @Override
    public String orderId() {
        return "";
    }

    @Override
    public String licensePlate() {
        return null;
    }

    @Override
    public void showToast(String content) {
        BToast.showText(this, content);
    }

    @Override
    public void closeDispose(Disposable disposable) {

    }

    @Override
    public void showProgress() {
        showLoding();
    }

    @Override
    public void hideProgress() {
        hideLoding();
    }

    @Override
    public void getMsg(MonthCarPayBean monthCarPayBean) {
        if (monthCarPayBean.getResult() != null) {
            Bundle bundle = new Bundle();
            bundle.putString("response", monthCarPayBean.getResult().getPay_url());
            startIntentActivity(WebPayActivity.class, bundle);
        }
    }

    @Override
    public void getMsg(GetParkingBean getParkingBean) {

    }

    @Override
    public void getMsg(CheckMonthParkBean checkMonthParkBean) {

    }

    @Override
    public void getMsg(AddMonthCarBean addMonthCarBean) {

    }

    @Override
    public void getMsg(GetMonthCarListBean getMonthCarListBean) {

    }

    List<String> monthList = new ArrayList<>();
    PaymentsBean payment;

    @Override
    public void getMsg(PaymentsBean paymentsBean) {
        communityTv.setText(paymentsBean.getResult().getParkCarExt().getParkName());
        carTv.setText(paymentsBean.getResult().getParkCarExt().getCarId());
        nameTv.setText(paymentsBean.getResult().getParkCarExt().getOwner());
        endTimeTv.setText(DateFormatUtils.getNormalTimeString(paymentsBean.getResult().getParkCarExt().getLocalEndTime(), "yyyy-MM-dd HH:mm", "yyyy-MM-dd"));
        startTimeTv.setText(DateFormatUtils.getNormalTimeString(paymentsBean.getResult().getParkCarExt().getNextStartTime(), "yyyy-MM-dd HH:mm", "yyyy-MM-dd"));

        monthList.clear();
        if (paymentsBean.getResult().getParkFees().size() > 0) {
            for (int i = 0; i < paymentsBean.getResult().getParkFees().size(); i++) {
                monthList.add(paymentsBean.getResult().getParkFees().get(i).getRealMonth() + "个月");
            }
            items = (String[]) monthList.toArray(new String[monthList.size()]);
            payment = paymentsBean;
        }

    }

    @Override
    public void getMsg(ComputeParkingBean computeParkingBean) {

    }

    @Override
    public void getMsg(FeeListBean feeListBean) {

    }
}
